package co.yixiang.yshop.module.member.service.userlevelgrowthvalueconfig;

import co.yixiang.yshop.framework.common.pojo.PageResult;
import co.yixiang.yshop.module.member.controller.admin.userlevelgrowthvalueconfig.vo.*;
import co.yixiang.yshop.module.member.convert.userlevelgrowthvalueconfig.UserLevelGrowthValueConfigConvert;
import co.yixiang.yshop.module.member.dal.dataobject.userlevelgrowthvalueconfig.UserLevelGrowthValueConfigDO;
import co.yixiang.yshop.module.member.dal.mysql.userlevelgrowthvalueconfig.UserLevelGrowthValueConfigMapper;
import co.yixiang.yshop.module.member.enums.LevelGrowthValueEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static co.yixiang.yshop.framework.common.exception.util.ServiceExceptionUtil.exception;
import static co.yixiang.yshop.module.member.enums.ErrorCodeConstants.USER_LEVEL_GROWTH_VALUE_CONFIG_NOT_EXISTS;

/**
 * 会员成长任务配置 Service 实现类
 *
 * @author admin
 */
@Service
@Validated
public class UserLevelGrowthValueConfigServiceImpl implements UserLevelGrowthValueConfigService {

    @Resource
    private UserLevelGrowthValueConfigMapper userLevelGrowthValueConfigMapper;

    @Override
    public Integer createUserLevelGrowthValueConfig(UserLevelGrowthValueConfigCreateReqVO createReqVO) {
        // 插入
        UserLevelGrowthValueConfigDO userLevelGrowthValueConfig = UserLevelGrowthValueConfigConvert.INSTANCE.convert(createReqVO);
        userLevelGrowthValueConfigMapper.insert(userLevelGrowthValueConfig);
        // 返回
        return userLevelGrowthValueConfig.getId();
    }

    @Override
    public void updateUserLevelGrowthValueConfig(UserLevelGrowthValueConfigUpdateReqVO updateReqVO) {
        // 校验存在
        validateUserLevelGrowthValueConfigExists(updateReqVO.getId());
        // 更新
        UserLevelGrowthValueConfigDO updateObj = UserLevelGrowthValueConfigConvert.INSTANCE.convert(updateReqVO);
        userLevelGrowthValueConfigMapper.updateById(updateObj);
    }

    @Override
    public void deleteUserLevelGrowthValueConfig(Integer id) {
        // 校验存在
        validateUserLevelGrowthValueConfigExists(id);
        // 删除
        userLevelGrowthValueConfigMapper.deleteById(id);
    }

    private void validateUserLevelGrowthValueConfigExists(Integer id) {
        if (userLevelGrowthValueConfigMapper.selectById(id) == null) {
            throw exception(USER_LEVEL_GROWTH_VALUE_CONFIG_NOT_EXISTS);
        }
    }

    @Override
    public UserLevelGrowthValueConfigDO getUserLevelGrowthValueConfig(Integer id) {
        return userLevelGrowthValueConfigMapper.selectById(id);
    }

    @Override
    public List<UserLevelGrowthValueConfigDO> getUserLevelGrowthValueConfigList(Collection<Integer> ids) {
        return userLevelGrowthValueConfigMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<UserLevelGrowthValueConfigDO> getUserLevelGrowthValueConfigPage(UserLevelGrowthValueConfigPageReqVO pageReqVO) {
        return userLevelGrowthValueConfigMapper.selectPage(pageReqVO);
    }

    @Override
    public List<UserLevelGrowthValueConfigDO> getUserLevelGrowthValueConfigList(UserLevelGrowthValueConfigExportReqVO exportReqVO) {
        return userLevelGrowthValueConfigMapper.selectList(exportReqVO);
    }

    @Override
    public UserLevelGrowthValueConfigDO getByType(String type) {
        return userLevelGrowthValueConfigMapper.selectOne(new LambdaQueryWrapper<UserLevelGrowthValueConfigDO>()
                .eq(UserLevelGrowthValueConfigDO::getType,type).last("limit 1"));
    }

    @Override
    public GrowthValueConfigDTO getGrowthValueConfig() {
        List<UserLevelGrowthValueConfigDO> list = this.getUserLevelGrowthValueConfigList(new UserLevelGrowthValueConfigExportReqVO());
        // Map<String, Integer> collect = list.stream().collect(Collectors.toMap(UserLevelGrowthValueConfigDO::getType, UserLevelGrowthValueConfigDO::getGrowthValue));
        Map<String, Integer> collect = list.stream()
                .collect(Collectors.toMap(
                        UserLevelGrowthValueConfigDO::getType,
                        UserLevelGrowthValueConfigDO::getGrowthValue,
                        (existing, replacement) -> existing));  // 如果有重复的键，保留原来的值
        GrowthValueConfigDTO dto = new GrowthValueConfigDTO();
        dto.setConsumeGrowthValue(collect.get(LevelGrowthValueEnum.CONSUME.getType()));
        dto.setInviteGrowthValue(collect.get(LevelGrowthValueEnum.INVITE.getType()));
        return dto;
    }

    @Override
    public void updateGrowthValueConfig(GrowthValueConfigDTO dto) {
        userLevelGrowthValueConfigMapper.update(null, Wrappers.lambdaUpdate(UserLevelGrowthValueConfigDO.class).eq(UserLevelGrowthValueConfigDO::getType, LevelGrowthValueEnum.CONSUME.getType()).set(UserLevelGrowthValueConfigDO::getGrowthValue, dto.getConsumeGrowthValue()));
        userLevelGrowthValueConfigMapper.update(null, Wrappers.lambdaUpdate(UserLevelGrowthValueConfigDO.class).eq(UserLevelGrowthValueConfigDO::getType, LevelGrowthValueEnum.INVITE.getType()).set(UserLevelGrowthValueConfigDO::getGrowthValue, dto.getInviteGrowthValue()));
    }

}
